package com.itcncp.ucenter.web.controller;

import com.itcncp.common.base.BaseController;
import com.itcncp.common.pingplusplus.model.Charge;
import com.itcncp.common.pingplusplus.model.Event;
import com.itcncp.common.pingplusplus.model.Transfer;
import com.itcncp.common.pingplusplus.model.Webhooks;
import com.itcncp.ucenter.rpc.api.UserAccountInfoService;
import com.itcncp.ucenter.rpc.api.UserRechangeOpJnlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
/**
 *
 * Created by shuitcncp on 2016/12/10.
 */
@Controller
@RequestMapping("/callback")
@Api(tags = "callback",value = "支付回调信息", description = "回调信息接口",
        produces = "produces",consumes = "consumes",basePath = "/callback")

public class CallBackController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger( CallBackController.class);

    @Autowired
    private UserRechangeOpJnlService userRechangeOpJnlService;

    @Autowired
    private UserAccountInfoService userAccountInfoService;

    @ApiOperation(value = "ping++支付回调信息")
    @RequestMapping(value = "/pingppCallBackPay", method = RequestMethod.POST)
    @ResponseBody
    public void pingppCallBackPay(HttpServletRequest request,HttpServletResponse response) throws IOException {

        request.setCharacterEncoding("UTF8");

        // 获得 http body 内容
        BufferedReader reader = request.getReader();
        StringBuffer buffer = new StringBuffer();
        String string;
        while ((string = reader.readLine()) != null) {
            buffer.append(string);
        }

        reader.close();

        //获取异步通知数据
        String eventStr = buffer.toString();

        // 将数据转换成Event对象
        Event event = Webhooks.eventParse(eventStr);

        //将数据转换成Charge对象
        Charge charge = (Charge) Webhooks.getObject(eventStr);

        try {
            if ("charge.succeeded".equals(event.getType())) {

                //充值订单号
                Long orderno = new Long( charge.getOrderNo() );
                //充值方式
                String channel = charge.getChannel();
                //第三方订单号
                String transactionno = charge.getTransactionNo();

                //回调通知类型
                String  type =  charge.getMetadata().get("type").toString();

                if(type.equals("1")){
                    //充值成功
                    userRechangeOpJnlService.pingppRechargeCallBack(orderno,transactionno,channel);
                }

                response.setStatus(200);

            } else if ("refund.succeeded".equals(event.getType())) {

                response.setStatus(200);

                System.out.println("退款成功通知");

            } else if ("recharge.succeeded".equals(event.getType())) {

                response.setStatus(200);

                System.err.println("充值成功通知");

            } else if ("transfer.succeeded".equals(event.getType())) {

                response.setStatus(200);

                System.err.println("付款成功时触发");

            } else if ("transfer.failed".equals(event.getType())) {

                response.setStatus(200);

                System.err.println("付款失败时触发");

            }else {

                response.setStatus(500);
            }

        } catch (Exception e) {

            response.setStatus(500);

        }

        response.setStatus(500);
    }


    @ApiOperation(value = "提现回调")
    @RequestMapping(value = "/pingppCallWithdrawals", method = RequestMethod.POST)
    @ResponseBody
    public void pingppCallWithdrawals(HttpServletRequest request,HttpServletResponse response) throws IOException {

        request.setCharacterEncoding("UTF8");

        // 获得 http body 内容
        BufferedReader reader = request.getReader();
        StringBuffer buffer = new StringBuffer();
        String string;
        while ((string = reader.readLine()) != null) {
            buffer.append(string);
        }

        reader.close();

        //获取异步通知数据
        String eventStr = buffer.toString();

        // 将数据转换成Event对象
        Event event = Webhooks.eventParse(eventStr);

        //将数据转换成Charge对象
        Transfer transfer = (Transfer) Webhooks.getObject(eventStr);

        try {

            //提现订单号
            String orderno = transfer.getOrderNo();

            if ("transfer.succeeded".equals(event.getType())) {

                System.out.println("企业付款成功");

                userAccountInfoService.cashinOpJnlState(orderno,3);

                response.setStatus(200);

            } else if ("transfer.failed".equals(event.getType())) {

                System.out.println("企业付款失败");

                userAccountInfoService.cashinOpJnlState(orderno,1);

                response.setStatus(200);

            } else {

                response.setStatus(500);
            }

        } catch (Exception e) {

            response.setStatus(500);

        }

        response.setStatus(500);
    }


}